/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年3月21日         luwei
 */

package com.coocaa.app.gamecenter.framework.ui;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.coocaa.framework.ui.BaseAdapter;
import com.coocaa.launcher.wallpaper.AnimatorUtils;
import com.tianci.appstore.R;
import com.tianci.appstore.utils.SkyConstants;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class StraggGridView extends ScrollView implements BaseAdapter.ObserverListener {
    private LinearLayout mMainLayout = null;
    private Context mContext = null;
    private BaseAdapter mAdapter = null;
    private List<LinearLayout> mChildLayouts = new ArrayList<LinearLayout>();
    private HashMap<LinearLayout, Integer> mAddNum = new HashMap<LinearLayout, Integer>();
    private List<View> mChildView = new ArrayList<View>();
    private GridLayoutParams mGridLayoutParams = null;

    public StraggGridView(Context context) {
        super(context);
        setPadding(0, 0, 0, 0);
        setFocusable(false);
        setFocusableInTouchMode(false);
        setThumbV(R.drawable.game_center_progress_bar);
        this.mContext = context;
        mMainLayout = new LinearLayout(context);
        mMainLayout.setOrientation(LinearLayout.HORIZONTAL);
        FrameLayout.LayoutParams params = new LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        params.topMargin = SkyConstants.Div(-41);
        mMainLayout.setLayoutParams(params);
        addView(mMainLayout);
    }

    public interface OnPageBundaryListener {
        public void pageUpBundary(int col);

        public void pageDownBundary(int col);
    }

    public static class GridLayoutParams {
        public int mColumnNum = 0;
        public int mHSpace = 0;
        public int mVSpace = 0;

        public GridLayoutParams() {

        }
    }

    public void setThumbV(int id) {
        try {
            Field mScrollCacheField = View.class.getDeclaredField("mScrollCache");
            mScrollCacheField.setAccessible(true);
            Object mScrollCache = mScrollCacheField.get(this); // scr is your Scroll View

            Field scrollBarField = mScrollCache.getClass().getDeclaredField("scrollBar");
            scrollBarField.setAccessible(true);
            Object scrollBar = scrollBarField.get(mScrollCache);

            Method method = scrollBar.getClass().getDeclaredMethod("setVerticalThumbDrawable", Drawable.class);
            method.setAccessible(true);
            // Set your drawable here.
            method.invoke(scrollBar, getResources().getDrawable(id));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setGridLayoutParams(GridLayoutParams params) {
        this.mGridLayoutParams = params;
        if (mGridLayoutParams.mColumnNum > 0) {
            for (int i = 0; i < mGridLayoutParams.mColumnNum; i++) {
                if (i == 0) {
                    addChildLayout(0);
                } else {
                    addChildLayout(mGridLayoutParams.mHSpace);
                }
            }
        }
    }

    private void addChildLayout(int marginLeft) {
        LinearLayout layout = new LinearLayout(mContext);
        layout.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.leftMargin = marginLeft;
        mMainLayout.addView(layout, params);
        mChildLayouts.add(layout);
        mAddNum.put(layout, 0);
    }

    public void setAnimation(int id) {
        AnimationSet set = new AnimationSet(true);
        set.addAnimation(AnimationUtils.loadAnimation(mContext, id));
        LayoutAnimationController controller = new LayoutAnimationController(set, 0.5f);
        controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
        for (int i = 0; i < mChildLayouts.size(); i++) {
            mChildLayouts.get(i).setLayoutAnimation(controller);
        }
    }

    public void setAdapter(BaseAdapter adapter) {
        this.mAdapter = adapter;
        mAdapter.registObserver(this);
        mAdapter.notifyDataSetChanaged();
    }

    public void showColumn(int col, boolean show) {
        if (mChildLayouts.size() > col) {
            if (show) {
                AnimatorUtils.alpha(mChildLayouts.get(col), 0, 1);
            } else {
                AnimatorUtils.alpha(mChildLayouts.get(col), 1, 0);
            }
        }
    }

    @Override
    public void onChanaged() {
        int count = mAdapter.getCount();
        for (int i = 0; i < count; i++) {
            int addTo = i % mChildLayouts.size();
            int addNum = mAddNum.get(mChildLayouts.get(addTo));
            if (addNum < mChildLayouts.get(addTo).getChildCount()) {
                mAdapter.getView(i, mChildLayouts.get(addTo).getChildAt(addNum), null);
            } else {
                View view = mAdapter.getView(i, null, null);
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
                if (params == null) {
                    params = new LinearLayout.LayoutParams(
                            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                }
                params.topMargin = mGridLayoutParams.mVSpace;
                mChildLayouts.get(addTo).addView(view, params);
            }
            addNum++;
            mAddNum.put(mChildLayouts.get(addTo), addNum);
        }
        for (LinearLayout layout : mChildLayouts) {
            int addNum = mAddNum.get(layout);
            int temp = layout.getChildCount() - addNum;
            if (temp > 0) {
                for (int i = addNum; i < layout.getChildCount(); i++) {
                    layout.removeViewAt(i);
                }
            }
            mAddNum.put(layout, 0);
        }
        if (count == 0)
            removeView(mMainLayout);
        else if (mMainLayout.getParent() == null)
            addView(mMainLayout);
    }

    public BaseAdapter getAdapter() {
        return mAdapter;
    }

    public boolean setFocus(int pos) {
        int temp1 = pos % mGridLayoutParams.mColumnNum;
        int temp2 = pos / mGridLayoutParams.mColumnNum;
        if (mChildLayouts.get(temp1).getChildAt(temp2) != null) {
            mChildLayouts.get(temp1).getChildAt(temp2).requestFocus();
            return true;
        }
        return false;
    }

    public View getChildItem(int pos) {
        int temp1 = pos % mGridLayoutParams.mColumnNum;
        int temp2 = pos / mGridLayoutParams.mColumnNum;
        return mChildLayouts.get(temp1).getChildAt(temp2);
    }
}
